import yaml
import requests
import logging

class Weixin:

    # 清理已有 handlers
    root_logger = logging.getLogger()
    for h in root_logger.handlers[:]:
        root_logger.removeHandler(h)
    logging.basicConfig(level=logging.INFO)

    _token = ''
    @classmethod
    def get_token(cls):
        if len(cls._token) == 0:
            cls._token = cls.get_token_new()
        return cls._token

    @classmethod
    def get_token_new(cls):
        conf = yaml.safe_load(open("Weixin.yaml"))
        logging.debug(conf["env"])
        r = requests.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken",
                     params={"corpid": conf["env"]["corpid"], "corpsecret": conf["env"]["secret"]}).json()
        return r["access_token"]